﻿using Asp.Versioning;
using FlyingEye.Factories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace FlyingEye.Controllers
{
    [Route("api/device-ops-service/v{version:apiVersion}/production-processes")]
    [ApiVersion("1.0")]
    [ControllerName("ProductionProcess")]
    [Authorize]
    public class ProductionProcessController : DefaultController
    {
        private readonly IDeviceHostRepository _deviceHostRepository;

        private readonly IProductionProcessRepository _productionProcessRepository;

        public ProductionProcessController(
            IDeviceHostRepository deviceHostRepository,
            IProductionProcessRepository productionProcessRepository)
        {
            _deviceHostRepository = deviceHostRepository;
            _productionProcessRepository = productionProcessRepository;
        }

        [HttpGet("{id}/device-hosts")]
        public async Task<ActionResult<List<DeviceHostResponse>>> GetDeviceHostsAsync(Guid id)
        {
            return await this.GetInvokeAsync(async () =>
            {
                var deviceHosts = await _deviceHostRepository.GetListAsync(item => item.ProductionProcessId == id);
                return ObjectMapper.Map<List<DeviceHostModel>, List<DeviceHostResponse>>(deviceHosts);
            });
        }

        [HttpGet("{id}", Name = "GetByProductionProcessId")]
        public async Task<ActionResult<ProductionProcessResponse>> GetAsync(Guid id)
        {
            return await this.GetInvokeAsync(async () =>
            {
                var productionProcess = await _productionProcessRepository.GetAsync(id);
                return ObjectMapper.Map<ProductionProcessModel, ProductionProcessResponse>(productionProcess);
            });
        }

        [HttpGet]
        public async Task<ActionResult<List<ProductionProcessResponse>>> GetListAsync()
        {
            return await this.GetInvokeAsync(async () =>
            {
                var productionProcesses = await _productionProcessRepository.GetListAsync();
                return ObjectMapper.Map<List<ProductionProcessModel>, List<ProductionProcessResponse>>(productionProcesses);
            });
        }

        [HttpPost]
        public async Task<ActionResult<ProductionProcessResponse>> InsertAsync([FromBody] CreateProductionProcessRequest request)
        {
            return await this.InsertInvokeAsync(async () =>
            {
                var productionProcess = ObjectMapper.Map<CreateProductionProcessRequest, ProductionProcessModel>(request);
                productionProcess = await _productionProcessRepository.InsertAsync(productionProcess);
                return ObjectMapper.Map<ProductionProcessModel, ProductionProcessResponse>(productionProcess);
            }, "GetByProductionProcessId", e => new { e.Id });
        }

        [HttpPut]
        public async Task<ActionResult<ProductionProcessResponse>> UpdateAsync([FromBody] UpdateProductionProcessRequest request)
        {
            return await this.PutInvokeAsync(async () =>
            {
                var productionProcess = ObjectMapper.Map<UpdateProductionProcessRequest, ProductionProcessModel>(request);
                productionProcess = await _productionProcessRepository.UpdateAsync(productionProcess);
                return ObjectMapper.Map<ProductionProcessModel, ProductionProcessResponse>(productionProcess);
            });
        }

        [HttpDelete("{id}")]
        public async Task<NoContentResult> DeleteAsync(Guid id)
        {
            return await this.DeleteInvokeAsync(async () =>
            {
                await _productionProcessRepository.DeleteAsync(id);
            });
        }
    }
}
